package com.kude.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kude.entity.Article;
import com.kude.entity.ArticleCategory;
import com.kude.manager.ArticleManager;
import com.kude.manager.ConditionException;
import com.kude.manager.OperException;
import com.kude.manager.support.InCondition;
import com.kude.manager.support.QuerySupport;
import com.kude.util.BeanFactory;

public final class ArticleService {
	
	private ArticleManager articleManager;
	
	/**
	 * 根据ID获取文章
	 * 
	 * @param id
	 * @return
	 * @throws OperException
	 */
	public Article getArticle(int id) throws OperException {
		return articleManager.get(id);
	}
	
	/**
	 * 更新文章
	 * 
	 * @param article
	 * @throws OperException
	 */
	public void updateArticle(Article article) throws OperException {
		articleManager.update(article);
	}
	
	/**
	 * 根据ID删除文章
	 * @param articleManager
	 */
	public void deleteArticle(int id) throws OperException{
		articleManager.delete(id);
	}
	
	/**
	 * 增加文章
	 * 
	 */
	public void addArticle(Article article) throws OperException{
		articleManager.add(article);
	}
	
	/**
	 * 根据类别ID获取文章列表并分页
	 * @param articleManager
	 * @throws OperException 
	 */
	public List<Article> getArticleListByCategory(int pageNow, int pageSize,int categoryId) throws OperException{
		if(pageNow <= 0 && pageSize <= 0){
			return null;
		}
		int offset = (pageNow - 1) * pageSize;
		int limit = pageSize;
		SimpleExpression res = Restrictions.eq("articleCategory.articleCategoryId", categoryId);
		Order order = Order.desc("time");
		return articleManager.find(order, offset, limit, res);
	}
	
	/**
	 * 根据类别ID集合获取文章列表并分页，用于显示所属较大类别的筛选
	 * @param pageNow 
	 * @param pageSize 
	 * @param List<ArticleCategory>
	 * @throws OperException 
	 */
	public List<Article> getArticleListByCategoryIdList(int start, int limit,List<ArticleCategory> articleCategory) throws OperException{
		if(start <= 0 && limit <= 0){
			return null;
		}
		if(articleCategory.size() == 0 ){
			return null;
		}
		int offset = start;
		//HashMap<String, Integer> categoryMap = new HashMap();
		List<Integer> ids = new ArrayList<Integer>();
		for(int i = 0;i<articleCategory.size();i++){
			System.out.println(articleCategory.get(i).getArticleCategoryId()+"id!~~~~~~~~~~~~~");
			ids.add(articleCategory.get(i).getArticleCategoryId());
			//categoryMap.put("articleCategory.articleCategoryId", articleCategory.get(i).getArticleCategoryId());
		}
		Order order = Order.desc("time");
		return articleManager.find(order, offset, limit, Restrictions.in("articleCategory.articleCategoryId", ids));
	}
	
	/**
	 * 获取显示在首页的最新动态的文章
	 * @param pageNow
	 * @param pageSize
	 * @param isDisplayOnHomePage 
	 * @return
	 * @throws OperException
	 */
	public List<Article> getArticleByIsDisplayOnHomePage(int pageNow, int pageSize,int isDisplayOnHomePage) throws OperException{
		if(pageNow <= 0 && pageSize <= 0){
			return null;
		}
		int offset = (pageNow - 1) * pageSize;
		int limit = pageSize;
		SimpleExpression res = Restrictions.eq("isDisplayOnHomePage", isDisplayOnHomePage);
		Order order = Order.desc("time");
		return articleManager.find(order, offset, limit, res);
	}
	
	/**
	 * 获取首页大图片的文章
	 * @param pageNow
	 * @param pageSize
	 * @return
	 * @throws OperException
	 */
	public List<Article> getArticleByImageURLNullOrNot(int pageNow, int pageSize) throws OperException{
		if(pageNow <= 0 && pageSize <= 0){
			return null;
		}
		int offset = (pageNow - 1) * pageSize;
		int limit = pageSize;
		Order order = Order.desc("time");
		return articleManager.find(order, offset, limit, Restrictions.isNotNull("imagePath"));
	}
	
	public int getTotalByCategoryIdList(List<Integer> categoryIds) throws OperException, ConditionException{
		return articleManager.getTotal(new QuerySupport().and(new InCondition("category_id",categoryIds)));
	}

	public void setArticleManager(ArticleManager articleManager) {
		this.articleManager = articleManager;
	}

	
	
}
